home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / eText.subproj / XTAction.m < prev   
Encoding:
Text File  |  1995-08-01  |  4.1 KB  |  231 lines

  1. /* XText 0.9 7/20/95  beta 1
  2. Paul A. Griffin
  3.  
  4. pgriffin@tiac.net (home->NextMail/Mime/text welcome)
  5. */
  6. /*    This file is part of the XText package (version 0.8)
  7.     Mike Dixon, April 1992
  8.     
  9.     Copyright (c) 1992 Xerox Corporation.  All rights reserved.
  10.  
  11.     Use and copying of this software and preparation of derivative works based
  12.     upon this software are permitted.  This software is made available AS IS,
  13.     and Xerox Corporation makes no warranty about the software or its
  14.     performance.
  15. */
  16.  
  17. #import "XTAction.h"
  18. #import "eText.Class.h"
  19. #import "Kludges.subproj/ErrorStream.h"
  20. #import <appkit/Application.h>
  21. #import <stdio.h>
  22. #import <string.h>
  23. #import <appkit/NXCType.h>
  24.  
  25. @implementation XTAction
  26.  
  27. static id undefined_action = 0;
  28.  
  29. + undefinedAction
  30. {
  31.     if (!undefined_action)
  32.         undefined_action = [[XTAction allocFromZone:[NXApp zone]] init];
  33.     return undefined_action;
  34. }
  35.  
  36. - applyTo:xtext event:(NXEvent *)event
  37. {
  38.     [xtext unboundKey];
  39.     return self;
  40. }
  41.  
  42. @end
  43.  
  44. @implementation XTMsg0Action
  45.  
  46. - initSel:(SEL)sel
  47. {
  48.     [super init];
  49.     action_sel = sel;
  50.     return self;
  51. }
  52.  
  53. - applyTo:xtext event:(NXEvent *)event
  54. {
  55.     return [xtext perform:action_sel];
  56. }
  57.  
  58. @end
  59.  
  60. @implementation XTMsg1Action
  61.  
  62. - initSel:(SEL)sel arg:(int)arg
  63. {
  64.     [super init];
  65.     action_sel = sel;
  66.     action_arg = arg;
  67.     return self;
  68. }
  69.  
  70. - applyTo: xtext event:(NXEvent *)event
  71. {
  72.     return [xtext perform:action_sel with:(id)action_arg];
  73. }
  74.  
  75. @end
  76.  
  77. @implementation XTMsg2Action
  78.  
  79. - initSel:(SEL)sel arg:(int)arg1 arg:(int)arg2
  80. {
  81.     [super init];
  82.     action_sel = sel;
  83.     action_arg1 = arg1;
  84.     action_arg2 = arg2;
  85.     return self;
  86. }
  87.  
  88. - applyTo: xtext event:(NXEvent *)event
  89. {
  90.     return [xtext perform:action_sel
  91.                     with:(id)action_arg1 with:(id)action_arg2];
  92. }
  93.  
  94. @end
  95.  
  96. @implementation XTDispatchAction
  97.  
  98. - init
  99. {
  100.     charCode k;
  101.  
  102.     [super init];
  103.  
  104.     for (k=0; k<CHAR_CODES; ++k) actions[k] = nil;
  105.     
  106.     return self;
  107. }
  108.  
  109. // provided for backward compatability
  110. - initBase:(const char *)base estream:errs
  111. {
  112.     charCode k;
  113.  
  114.     [super init];
  115.     for (k=0; k<CHAR_CODES; ++k) actions[k] = nil;
  116.     if (!strcmp(base,"none")) {}
  117.  
  118. //    else if (!strcmp(base,"other_base"))
  119. //        initbase_other_base(actions)
  120.  
  121.     else {}
  122.     return self;
  123. }
  124.  
  125.  
  126. // a convenience method to loading keybindings
  127. - loadFromFile:(char *)fullName estream:errs
  128. {
  129.     FILE *fp;
  130.     int i;
  131.     char line[256];
  132.  
  133.     fp = fopen(fullName, "r");
  134.     
  135.     if(!fp){
  136.         char msg[100];
  137.         sprintf(msg,"Cannot read %s", fullName);
  138.         [(errs ? errs : [ErrorStream default]) report:msg];
  139.         return self;
  140.     }
  141.     
  142.     while(fgets(line, 256, fp) != NULL){
  143.         if(line[0] == '#' ){} // a comment
  144.         else{
  145.             for(i=0;i<strlen(line);i++){
  146.                 if(line[i] == '\n' || line[i] == '\t') line[i] = ' ';
  147.             }
  148.             [self addBindings:line estream:errs];
  149.         }
  150.     }
  151.                 
  152.     return self;
  153. }
  154.  
  155. - bindKey:(charCode)key toAction:action estream:errs
  156. {
  157.     if ((key < 0) || (key >= CHAR_CODES)) {
  158.         char msg[40];
  159.         sprintf(msg, "Invalid key code: %d", key);
  160.         [(errs ? errs : [ErrorStream default]) report:msg];
  161.     } else
  162.         actions[key] = action;
  163.     return self;
  164. }
  165.  
  166. - applyTo:xtext event:(NXEvent *)event
  167. {
  168.     charCode k = event->data.key.charCode << NUM_MASKS;
  169.     id action;
  170.  
  171.     if ((k >= 0) && (k < CHAR_CODES)) {
  172.         if (event->flags & NX_ALPHASHIFTMASK)    k += 1;
  173.         if (event->flags & NX_SHIFTMASK)          k += 2;
  174.         if (event->flags & NX_CONTROLMASK)        k += 4;
  175.         if (event->flags & NX_ALTERNATEMASK)      k += 8;
  176.         if (event->flags & NX_COMMANDMASK)        k += 16;
  177.         if (event->flags & NX_NUMERICPADMASK)     k += 32;
  178.         if (event->flags & NX_HELPMASK)           k += 64;
  179.  
  180.         action = actions[k];
  181.         if (action) return [action applyTo:xtext event:event];
  182.         
  183.         /* also try action without the caps lock bit on */
  184.         if(event->flags & NX_ALPHASHIFTMASK){
  185.             k-=1;
  186.             action = actions[k];
  187.             if (action) return [action applyTo:xtext event:event];
  188.         }
  189.     }
  190.     return nil;
  191. }
  192.  
  193. @end
  194.  
  195. @implementation XTEventMsgAction
  196.  
  197. - initSel:(SEL)sel
  198. {
  199.     [super init];
  200.     action_sel = sel;
  201.     return self;
  202. }
  203.  
  204. - applyTo:xtext event:(NXEvent *)event
  205. {
  206.     return [xtext perform:action_sel with:(id)event];
  207. }
  208.  
  209. @end
  210.  
  211. @implementation XTSeqAction
  212.  
  213. - initLength:(int)len actions:(XTAction **)acts
  214. {
  215.     [super init];
  216.     length = len;
  217.     actions = acts;
  218.     return self;
  219. }
  220.  
  221. - applyTo:xtext event:(NXEvent *)event
  222. {
  223.     int i;
  224.  
  225.     for (i=0; i<length; ++i)
  226.         [actions[i] applyTo:xtext event:event];
  227.     return self;
  228. }
  229.  
  230. @end
  231.